<?php

namespace app\admin\controller;

use app\admin\service\GoodsService;
use support\spark\model\goods\Goods;
use support\spark\model\goods\GoodsAttr;
use support\spark\model\goods\GoodsCate;
use support\spark\model\goods\GoodsContent;
use support\spark\model\goods\GoodsRule;
use support\spark\model\goods\GoodsRuleExtend;
use support\Request;
use support\View;

class GoodsController extends Curd
{

    public function index()
    {
        if (request()->isAjax()) {
            $goodsService = new GoodsService();
            $res = $goodsService->getList(request()->all());
            return sparkSuccess($res);
        }
        $goodsCateModel = new GoodsCate();
        $cate = $goodsCateModel->getAllList([
            'status' => 1
        ], ['id', 'pid', 'name']);
        View::assign([
            'cate' => json_encode(makeTree($cate))
        ]);
        return view('goods/index');
    }

    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $goodsService = new GoodsService();
            $res = $goodsService->addGoods($param, 'add');
            return sparkSuccess($res);
        }
        $goodsService = new GoodsService();
        View::assign($goodsService->getBaseParam());
        return view('goods/add');
    }

    public function edit()
    {

        if (request()->isAjaxPost()) {

            $param = request()->post();
            $goodsService = new GoodsService();
            $res = $goodsService->editGoods($param, 'edit');
            return sparkSuccess($res);
        }
        $id = request()->input('id');
        $goodsModel = new Goods();
        $goodsRuleModel = new GoodsRule();
        $goodsRuleExtendModel = new GoodsRuleExtend();
        $goodsContentModel = new GoodsContent();
        $goodsAttrModel = new GoodsAttr();
        View::assign([
            'info' => json_encode($goodsModel->getInfoByWhere([
                'id' => $id
            ])),
            'ruleData' => json_encode($goodsRuleModel->getInfoByWhere([
                'goods_id' => $id
            ])),
            'extend' => json_encode($goodsRuleExtendModel->getAllList([
                'goods_id' => $id
            ], ['*'], ['id'=> 'asc'])),
            'content' => json_encode($goodsContentModel->getInfoByWhere([
                'goods_id' => $id
            ])),
            'attrData' => json_encode($goodsAttrModel->getAllList([
                'goods_id' => $id
            ], ['*'], ['id'=> 'asc']))
        ]);
        $goodsService = new GoodsService();

        View::assign($goodsService->getBaseParam());
        return view('goods/edit');
    }

    /**
     * 上下架
     */
    public function shelf()
    {
        $param = request()->post();
        $goodsModel = new Goods();

        $goodsModel->updateByIds([
            'is_show' => $param['is_show']
        ], $param['ids']);

        return sparkSuccess(dataReturn(0,'操作成功'));
    }

    /**
     * 删除商品
     */
    public function del()
    {
        $goodsService = new GoodsService();
        return sparkSuccess($goodsService->delGoods(request()->input('id')));
    }

    /**
     * 恢复商品
     * @return \support\Response
     */
    public function recover()
    {
        $id = request()->input('id');
        $goodsModel = new Goods();

        $goodsModel->updateById([
            'is_del' => 2
        ], $id);

        return sparkSuccess(dataReturn(0,'操作成功'));
    }

}
